<template>
	<view>
		<view class="header-view">
			<view class="header-view-logo">
				<image class="header-logo-image" :src="shop_logo" mode="scaleToFill"></image>
				<view class="header-logo-text">{{shop_name}}</view>
			</view>
			<view class="header-saoma" @click="scan">
				<view class="header-saoma-bg">
					<image class="header-saoma-image" src="../../../static/tab/duoshang-02.png" mode=""></image>
				</view>
				<!-- <view class="header-saoma-title" style="margin-bottom: 50rpx;">扫码核销</view> -->
				<view class="header-saoma-btn">扫码核销</view>
				<!-- <view class="header-saoma-btn" @click.stop="goToUrl">输入兑换码</view> -->
			</view>
			<!-- <view class="header-saoma-inputView">
				
			</view> -->
		</view>

		<view>
			<view style="padding: 30rpx;">
				<view class="saoma-title">
					兑换记录
				</view>
			</view>
			<view class="page">
				<view class="general-view margin-lr padding radius-16" style="margin-top: 32rpx;padding: 20rpx;">
					<view class="exchange" style="padding-bottom: 32rpx;">
						<view class="flex align-center ">
							<text class="text-333 datalist" :class="index == tabIndex?'isActive':''"
								v-for="(item,index) in tab" :key="index"
								@tap="handeTab(item, index)">{{item.name}}</text>

						</view>
						<view class="" style="font-size: 28rpx;margin-top: 32rpx;color:#666666;" v-if="month">
							{{month}}月份共兑换{{total}}张
						</view>
					</view>
					<!-- 兑换商品 -->
					<view class="bg-white padding" style="border-radius: 16rpx;margin-bottom:24rpx;"
						v-for="(item, index) in list" :key="index">
						<view class="bg-list flex " style="justify-content: space-between;">
							<view class="flex">
								<text class="text-size">兑换码：{{item.picke_code}}</text>
							</view>
							<text style="color: #0684e8;font-size: 28rpx;">已兑换</text>
						</view>
						<view style="justify-content: space-between;">
							<view class="flex" style="border-bottom:1rpx solid #eee;padding: 32rpx 0;">
								<image :src="item.goods_thumb" mode=""
									style="width: 140rpx;height: 140rpx;margin-right: 16rpx;flex-shrink: 0;border-radius: 16rpx;">
								</image>
								<view class="conten">
									<view class="text-cut"
										style="font-size: 28rpx;font-family: PingFang SC-Medium, PingFang SC;font-weight: 500;color: #333333;">
										{{item.goods_name}}</view>
									<view class="flex margin-top-16">
										<view class="flex" style="align-items: center">
											<image src="https://xcxadmin.jgwxch.com/h5img/image/010.png" mode=""
												style="width: 22rpx;height: 22rpx;margin-right: 16rpx;"></image>
											<text style="font-size: 20rpx;">无需预约</text>
										</view>
										<!-- <view class="flex" style="align-items: center;margin-left: 12rpx;">
			                    <image src="https://xcxadmin.jgwxch.com/h5img/image/010.png" mode="" style="width: 22rpx;height: 22rpx;margin-right: 16rpx;"></image>
			                    <text style="font-size: 20rpx;">随时退</text>
			                </view> -->
										<view class="flex" style="align-items: center;margin-left: 12rpx;">
											<image src="https://xcxadmin.jgwxch.com/h5img/image/010.png" mode=""
												style="width: 22rpx;height: 22rpx;margin-right: 16rpx;"></image>
											<text style="font-size: 20rpx;">积分抵</text>
										</view>
									</view>
									<view class="flex margin-top-16" v-if="item.is_goods_type != 1">
										<view class="flex" style="align-items: center">
											<text
												style="font-size: 28rpx;font-family: Lato-Bold, Lato;font-weight: bold;color: #FF5C5C;margin-right: 16rpx;">￥{{item.shop_price}}</text>
											<text
												style="font-size: 20rpx;text-decoration: line-through;">￥{{item.market_price}}</text>
										</view>

									</view>
								</view>
							</view>
							<view class="flex" style="margin-top:20rpx;">
								<text class="text-cl">兑换时间：{{item.create_time}}</text>
							</view>
						</view>

					</view>
				</view>
				<fu-empty-ui v-if="list.length == 0 && isInit"></fu-empty-ui>
				<uni-load-more v-if="list.length > 5" :status="status"></uni-load-more>
				<fu-popup v-model="visible" mode="center" :maskClick="true">

					<view class="wrap">
						<view class="coupon_content flex flex-wrap justify-center">
							<view class="">
								兑换商品
							</view>
							<scroll-view scroll-y="true" style="height:620rpx;">
								<view class="flex" style="justify-content: space-between;"
									v-for="(item, index) in goodsList" :key="index">
									<view class="flex margin-top" style="align-items: center;">
										<view class="margin-right-32 flex-shrink" @click="selectGoods(item)">
											<image src="/static/checked.png" mode="scaleToFill"
												style="width: 36rpx;height: 36rpx;" v-if="item.isSelect" />
											<image src="/static/unchecked.png" mode="scaleToFill"
												style="width: 36rpx;height: 36rpx;" v-else />
										</view>
										<image :src="item.goods_thumb" mode=""
											style="width: 140rpx;height: 140rpx;margin-right: 24rpx;flex-shrink: 0;border-radius: 16rpx;">
										</image>
										<view class="conten">
											<view
												style="font-size: 28rpx;font-family: PingFang SC-Medium, PingFang SC;font-weight: 500;color: #333333;    overflow: hidden;white-space: nowrap;text-overflow: ellipsis;width:390rpx;">
												{{item.goods_name}}
											</view>
											<view class="flex conten">
												<view class="flex" style="align-items: center">
													<image src="https://xcxadmin.jgwxch.com/h5img/image/010.png" mode=""
														style="width: 22rpx;height: 22rpx;margin-right: 16rpx;"></image>
													<text style="font-size: 20rpx;
				color: #666666;">无需预约</text>
												</view>
												<!-- <view class="flex" style="align-items: center;margin-left: 12rpx;">
				                                <image src="https://xcxadmin.jgwxch.com/h5img/image/010.png" mode="" style="width: 22rpx;height: 22rpx;margin-right: 16rpx;"></image>
				                                <text style="font-size: 20rpx;
				color: #666666;">随时退</text>
				                            </view> -->
												<view class="flex" style="align-items: center;margin-left: 12rpx;">
													<image src="https://xcxadmin.jgwxch.com/h5img/image/010.png" mode=""
														style="width: 22rpx;height: 22rpx;margin-right: 16rpx;"></image>
													<text style="font-size: 20rpx;
				color: #666666;">积分抵</text>
												</view>
											</view>
											<view class="flex conten">
												<view class="flex" style="align-items: center">
													<text
														style="font-size: 28rpx;font-family: Lato-Bold, Lato;font-weight: bold;color: #FF5C5C;margin-right: 16rpx;">￥{{item.shop_price}}</text>
													<text style="font-size: 20rpx;text-decoration: line-through;
				color: #999999;">￥{{item.market_price}}</text>
												</view>

											</view>
										</view>
									</view>
								</view>
							</scroll-view>
							<!-- 立即领取 -->
							<view class="footer-bottom text-lg" @click="exchange">
								立即兑换
							</view>
						</view>
					</view>
					<view style="width: 100%;margin-top: 70rpx;" class="flex justify-center">
						<!-- <image @click="visible = false" src="./static/close.png" style="width: 64rpx;height: 64rpx;" mode="">
							</image> -->
					</view>
				</fu-popup>
				<!-- 网络检测 start -->
				<fu-notwork></fu-notwork>
				<!-- 网络检测 end -->
			</view>
		</view>
	</view>
</template>

<script>
	import richText from "@/common/utils/richText.js";
	const year = new Date().getFullYear();
	const m = new Date().getMonth() + 1;
	const month = m < 10 ? `0${m}` : m
	const nowMonth = `${year}-${month}`
	// 注意：1月份返回的是0
	const m2 = new Date().getMonth();
	let month2 = parseInt(m2);
	if (month2 == 0) {
		month2 = 12;
	}
	export default {
		data() {
			return {
				picke_code: '',
				goodsList: [],
				visible: false,
				isShowLoading: false, //页面加载loading
				id: "",
				detail: "",
				content: "",
				body: "",
				tab: [{
						value: 'all',
						name: '全部'
					},
					{
						value: nowMonth,
						name: nowMonth,
						month: m
					},
					{
						value: this.getUpMonth(),
						name: this.getUpMonth(),
						month: month2
					},
				],
				tabIndex: 0,
				minixPagingListsApi: global.apiUrls.post6406941d1e71c, //列表请求地址
				allowOnloadGetList: false, //是否允许请求
				pageingListApiMethod: "post", //请求方式
				date: 'all',
				page: 1,
				size: 10,
				status: "more",
				list: [],
				isInit: false,
				total: 0,
				month: '',
				shop_logo: '',
				shop_name: ''
			}
		},
		computed: {
			write_off_num() {
				let arr = this.goodsList.filter(item => item.isSelect)
				return arr.length
			}
		},
		onLoad(options) {
			this.id = options.id;
			this.allowOnloadGetList = true
			this.getList();
			this.getShopInfo()
		},
		onShow() {

		},
		onPullDownRefresh() {
			this.status = 'more';
			this.page = 1;
			this.getList();
		},
		onReachBottom() {
			this.getList()
		},
		methods: {
			duihuan() {
				if (this.isCheck) return
				if (!this.picke_code) {
					this.$message.info('请输入兑换码')
					return
				}
				// if (!this.verify_code) {
				// 	this.$message.info('请输入验证码')
				// 	return
				// }
				this.isCheck = true
				this.$api.post(global.apiUrls.post64058d51a7f80, {
					picke_code: this.picke_code
				}).then(res => {
					if (res.data.code == 1) {
						console.log(res.data.data)
						let data = res.data.data
						data.forEach(item => {
							item.isSelect = true
						})
						this.goodsList = data
						this.isCheck = false
						this.visible = true
					} else {
						this.isCheck = false
						this.$message.info(res.data.msg)
					}
				}).catch(() => {
					this.isCheck = false
				})
			},
			selectGoods(item) {
				console.log(item)
				item.isSelect = !item.isSelect
			},
			// 立即兑换
			exchange() {
				if (this.isCheck) return;
				if (this.write_off_num < 1) {
					this.$message.info('请选择兑换商品')
					return
				}
				
				this.isCheck = true
				this.$api.post(global.apiUrls.post640590e4aaa78, {
					picke_code: this.picke_code,
					write_off_num: this.write_off_num
				}).then(res => {
					if (res.data.code == 1) {
						this.$message.info(res.data.msg)
						this.picke_code = ''
						this.visible = false
						
						this.status = 'more';
						this.page = 1;
						this.getList()
					}
					setTimeout(() => {
						this.isCheck = false
					}, 800)
				}).catch(() => {
					this.isCheck = false
				})
			},
			getShopInfo() {
				this.$api.post(global.apiUrls.getShopIndexInfo, {
					merchant_id: this.$store.getters.userInfo.id,
					date_selection: this.tabType
				}).then(res => {
					let info = res.data.data;
					this.shop_logo = info.shopLogo;
					this.shop_name = info.shopName;
				})
			},
			goToUrl() {
				uni.navigateTo({
					url: '/pages/tab/scan-code/input-code'
				})
			},
			scan() {
				uni.scanCode({
					success: res => {
						console.log(res.result)
						this.picke_code = res.result
						this.$api.post(global.apiUrls.post64058d51a7f80, {
							picke_code: res.result
						}).then(res => {
							if (res.data.code == 1) {
								console.log(res.data.data)
								let data = res.data.data
								console.log(data)
								data.forEach(item => {
									item.isSelect = true
								})
								this.goodsList = data
								this.visible = true
							} else {
								this.$message.info(res.data.msg)
							}
						})
					}
				})
			},
			handeTab(item, index) {
				this.tabIndex = index;
				this.date = item.value
				this.status = 'more';
				this.page = 1;
				this.month = item.month ? item.month : ''
				this.getList();
			},
			getList() {
				if (this.status != "more") return;
				this.status = "loading";
				this.$api.post(global.apiUrls.post6406941d1e71c, {
					date: this.date,
					page: this.page,
					list_rows: this.size
				}).then(res => {
					this.isShowLoading = false
					this.isInit = true
					if (res.data.code == 1) {
						var totalSize = res.data.data.list.total;
						var curPageData = res.data.data.list.data;
						this.total = res.data.data.list.total
						if (this.page == 1) this.list = [];
						this.list = this.list.concat(curPageData);
						if (this.list.length < totalSize) {
							this.status = 'more';
							this.page++;
						} else {
							this.status = 'noMore';
						}
					} else {
						this.$message.info(res.data.msg);
					}
				}).catch(() => {
					this.isShowLoading = false
				})
			},
			// 获取上月月份
			getUpMonth() {
				let date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth();

				let year2 = year;
				// 注意：1月份返回的是0
				let month2 = parseInt(month);
				if (month2 == 0) {
					year2 = parseInt(year2) - 1;
					month2 = 12;
				}
				if (month2 < 10) {
					month2 = '0' + month2;
				}
				var m = year2.toString();
				var n = month2.toString();
				var t2 = m + "-" + n;
				// 最终返回格式2022-12
				return t2;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.footer-bottom {
		background: #FC6497;
		border-radius: 44rpx;
		color: #FFFFFF;
		padding: 22rpx 96rpx;
		margin-top: 64rpx;
	}
	.wrap {
		width: 680rpx;
		overflow: hidden;
		background-size: 100%;
		background-repeat: no-repeat;
		// background: url(~@/static/discount.png)no-repeat;
		background-color: #FFFFFF;
		background-size: 100% 100%;
		height: 898rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 16rpx;
	
		.close_Popup {
			position: fixed;
			right: 30rpx;
			top: 30rpx;
			font-size: 48rpx;
		}
	
		.coupon_content {
			// width: 540rpx;
			font-size: 42rpx;
			// height: 100%;
			// padding: 300rpx 50rpx 20rpx 20rpx;
			// padding-top: 32rpx;
			padding: 24rpx;
	
			.coupon_title {
				padding-bottom: 40rpx;
			}
	
			.items {
				width: 100%;
				height: 144rpx;
				background-size: 100% 100%;
				display: flex;
				margin-bottom: 20rpx;
	
				.left {
					width: 170rpx;
					height: 100%;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 46rpx;
					color: #D9361E;
				}
	
				.center {
					flex: 1;
					display: flex;
					flex-direction: column;
					// margin-left: 10rpx;
	
					text {
						font-size: 28rpx;
						color: #333333;
					}
	
					view {
						font-size: 20rpx;
						color: #693313;
					}
				}
	
				.right {
					width: 120rpx;
					height: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
	
					.buttons {
						width: 80rpx;
						color: #6A8D6C;
						font-size: 30rpx;
						line-height: 46rpx;
						text-align: center;
					}
	
					.disabled {
						opacity: 0.5;
					}
				}
			}
		}
	}
	.saoma-title {
		font-size: 15px;
		color: #000;
		margin-bottom: 30rpx;
	}

	.header-saoma-inputView {
		display: flex;
		align-items: center;
	}

	.header-saoma-btn {
		width: 400rpx;
		height: 100rpx;
		padding: 0 20rpx;
		background-color: #FEF9EB;
		border-radius: 10rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-weight: 400;
		font-size: 30rpx;
		color: #FC6296;
	}

	.header-saoma-input {
		flex: 1;
		height: 100rpx;
		padding: 0 20rpx;
		background-color: #FEF9EB;
		border-radius: 10rpx;
		font-weight: 400;
		font-size: 30rpx;
		color: #FC6296;
		margin-right: 20rpx;
	}

	.header-saoma-title {
		font-weight: 400;
		font-size: 30rpx;
		color: #FFFFFF;
	}

	.header-saoma {
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-bottom: 30rpx;
	}

	.header-saoma-bg {
		width: 240rpx;
		height: 240rpx;
		background: #FEF9EB;
		opacity: 0.24;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 50%;
		margin: 50rpx 0;
	}

	.header-saoma-image {
		width: 137rpx;
		height: 137rpx;
		flex-shrink: 0;
	}

	.header-logo-text {
		font-weight: 400;
		font-size: 30rpx;
		color: #FFFFFF;
	}

	.header-logo-image {
		width: 66rpx;
		height: 66rpx;
		border-radius: 50%;
		flex-shrink: 0;
		margin-right: 20rpx;
	}

	.header-view-logo {
		display: flex;
		align-items: center;
	}

	.header-view {
		background-color: #FC6296;
		padding: 30rpx;
	}

	.page {
		// background-color: white;
		height: 100%;
		height: 100vh;
	}

	/* 文章标题 start */
	.title {
		padding: 32upx 32upx 0;
		/* height: 99rpx; */
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 500;
		line-height: 54rpx;
		color: #333333;
		opacity: 1;
	}

	.time {
		padding: 0 32upx;
		height: 33rpx;
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 400;
		line-height: 33rpx;
		color: #999999;
		opacity: 1;
		margin-top: 18rpx;
	}

	/* 文章标题 end */

	/* 文章详情 start */
	.article-detail {
		margin: 32upx 32upx 24upx;
		color: #666666;
		font-size: 28upx;
		line-height: 48upx;
	}

	/* 文章详情 end */
	.active {
		border-radius: 44rpx;
		text-align: center;
		border: 1rpx solid #FC6296;
		padding: 8rpx 24rpx;
		color: #FC6296;
	}

	.exchange {

		// background: #fff;
		// padding: 56rpx 32rpx;
		// text-align: center;
		// border: 16rpx;
		// margin-bottom: 32rpx;
		.datalist {
			background: #fff;
			border-radius: 66rpx;
			padding: 0 32rpx;
			margin-right: 24rpx;
			color: #666666;
			height: 64rpx;
			line-height: 64rpx;
			box-sizing: border-box;
			// height: 88rpx;
			// background: #F6F7F9;
			// border-radius: 60rpx;
			// text-align: center;
			// justify-content: center;
		}

		.isActive {
			color: #FC6296;
			border: 1rpx solid #FC6296;
		}
	}

	.bg-list {
		// text-align: center;
		font-size: 32rpx;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: bold;
		color: #333333;
	}

	.conten {
		// margin: 16rpx 0;
		flex: 1;
		overflow: hidden;
	}

	.btn_cloce {
		position: fixed;
		left: 32rpx;
		right: 32rpx;
		bottom: 50rpx;
		background-color: #FC6296;

		// border: 1rpx solid #BFBFBF;
		border-radius: 55rpx;

		.btn {
			height: 88rpx;
			text-align: center;
			line-height: 88rpx;
			color: #fff;
			font-size: 32rpx;
		}
	}

	.text-size {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.text-cl {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}
</style>